﻿using Newtonsoft.Json;
using Rest.WebApiClient.Contracts;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebApiClient;

namespace Rest.API.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Init();
            RequestAsync().Wait();
            Console.ReadLine();
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        static void Init()
        {
            //HttpServer.Start(9999);

            //  注册IUserApi 配置其工厂
            HttpApi.Register<IUserApi>().ConfigureHttpApiConfig(c =>
            {
              
                // 可以替换的序列化工具
                c.JsonFormatter = null;
                c.XmlFormatter = null;
                c.KeyValueFormatter = null;

                // 参数验证和返回值验证，使用System.ComponentModel.DataAnnotations验证特性
                c.UseParameterPropertyValidate = false;
                c.UseReturnValuePropertyValidate = false;

                // 请求主机和HttpClient相关配置
                c.HttpHost = new Uri("https://localhost:44309/");
                c.HttpClient.Timeout = TimeSpan.FromMinutes(2d);

                // 格式相关配置
                c.FormatOptions.UseCamelCase = true;
                c.FormatOptions.DateTimeFormat = DateTimeFormats.ISO8601_WithMillisecond;

                // 响应缓存提供者配置，配合[CacheAttribute]来使用
                c.ResponseCacheProvider = null;

                // 服务提供者，实例一般由DI创建得到
                // 对于Asp.net core，此ServiceProvider应该为请求时创建的ServiceProvider，而不是ConfigureServices()创建的根ServiceProvider
                c.ServiceProvider = null;
                // 日志工厂，可以自主创建并赋值，如果保留为null，获取其实例时则从ServiceProvider获取 
                c.LoggerFactory = null;
            });
        }

        /// <summary>
        /// 请求接口
        /// </summary>
        /// <returns></returns>
        private static async Task RequestAsync()
        {
            var userApi = HttpApi.Resolve<IUserApi>();
            var user = await userApi.GetUserAPI("123");
            //var userV2 = await userApi.GetUserAPIV2(" WeatherForecast/GetUserInfo","123");
           
            //string value = "{\"name\":\"1\",\"id\":\"2\"}";
            //var about = await userApi.GetAboutAsync("api/Test/GetValue", value);
        }
    }
}
